package com.tingyun.sign.view.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tingyun.sign.activity.service.ActivityService;
import com.tingyun.sign.companyUser.controller.CompanyUserController;
import com.tingyun.sign.companyUser.service.CompanyUserService;
import com.tingyun.sign.core.entity.ScActivityBean;
import com.tingyun.sign.core.entity.ScUCompanyUserBean;
import com.tingyun.sign.core.entity.ScUManagerBean;
import com.tingyun.sign.core.repository.CompanyUserDao;
import com.tingyun.sign.utils.BaseController;
import com.tingyun.sign.utils.DateUtils;
import com.tingyun.sign.utils.Page;
import com.tingyun.sign.view.bean.TestBean;
import com.tingyun.sign.view.service.IIndexService;

@Controller
@RequestMapping("view")
public class IndexController extends BaseController {
	private static Logger log = LoggerFactory
			.getLogger(CompanyUserController.class);

	@Autowired
	private CompanyUserService companyUserService;

	@Autowired
	private IIndexService indexService;

	@Autowired
	private ActivityService activityService;
	
	@Autowired
	private CompanyUserDao companyUserDao;

	@RequestMapping("index")
	public String index() {
		indexService.index();
		return "index";
	}

	@RequestMapping("login")
	public String login() {
		indexService.index();
		return "login";
	}

	@RequestMapping(value = "sign")
	public ModelAndView list(HttpSession session, ScUCompanyUserBean bean) {
		ModelAndView mv = new ModelAndView();
		List<ScActivityBean> acs = activityService
				.findAllActivities(getSessionUser(session).getType());
		mv.addObject("acs", acs);
		mv.setViewName("sign");
		return mv;
	}

	@RequestMapping(value = "findPage")
	@ResponseBody
	public Map<String, Object> findCompanyUsers(ScUCompanyUserBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		Page<ScUCompanyUserBean> page = companyUserService.findUser(bean);
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResultList());
		return map;
	}
	
	@RequestMapping(value = "signIn")
	@ResponseBody
	public Map<String, Object> signIn(HttpSession session, ScUCompanyUserBean bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		ScUManagerBean manager = getSessionUser(session);
		bean.setMuser(manager.getId());
		Integer result = null;
		try {
			result = companyUserService.signIn(bean);
			System.out.println(result);
		} catch (Exception e) {
			map.put("info", e.getMessage());
		}
		map.put("result", result);
		return map;
	}
}
